package com.filemanager;

import javax.swing.*;
import javax.swing.tree.*;
import java.awt.*;
import java.io.File;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class FileTreePanel extends JPanel {
    private JTree tree;
    private File currentRootFile; // 当前根目录
    private JButton backOrSelectButton;   // 返回上一级/选择根目录 按钮

    public FileTreePanel() {
        // 顶部面板，包含按钮
        JPanel topPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
        backOrSelectButton = new JButton("选择根目录"); // 始终显示"选择根目录"
        topPanel.add(backOrSelectButton);

        // 选择初始根目录
        JFileChooser chooser = new JFileChooser();
        chooser.setDialogTitle("请选择根目录");
        chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
        int result = chooser.showOpenDialog(this);
        if (result == JFileChooser.APPROVE_OPTION) {
            currentRootFile = chooser.getSelectedFile();
        } else {
            this.setLayout(new BorderLayout());
            JLabel errorLabel = new JLabel("未选择根目录，无法显示文件树。", SwingConstants.CENTER);
            this.add(errorLabel, BorderLayout.CENTER);
            this.setEnabled(false);
            return;
        }

        // 初始化文件树
        tree = createTreeForRoot(currentRootFile);
        JScrollPane scrollPane = new JScrollPane(tree);

        // 按钮事件：每次点击都弹出选择器重新选择根目录
        backOrSelectButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                JFileChooser chooser = new JFileChooser();
                chooser.setDialogTitle("请选择新的根目录");
                chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
                int result = chooser.showOpenDialog(FileTreePanel.this);
                if (result == JFileChooser.APPROVE_OPTION) {
                    currentRootFile = chooser.getSelectedFile();
                    refreshTree();
                }
            }
        });

        // 主布局
        this.setLayout(new BorderLayout());
        this.add(topPanel, BorderLayout.NORTH);
        this.add(scrollPane, BorderLayout.CENTER);
    }

    // 创建指定根目录的树
    private JTree createTreeForRoot(File rootFile) {
        DefaultMutableTreeNode rootNode = new DefaultMutableTreeNode(rootFile.getName());
        FileUtils.createChildren(rootFile, rootNode);
        JTree jtree = new JTree(rootNode);
        // 注入右键菜单，传递this以便菜单能获取当前根目录
        new FileTreeContextMenu(jtree, this);
        return jtree;
    }

    // 刷新树结构，显示当前根目录
    private void refreshTree() {
        this.removeAll();
        tree = createTreeForRoot(currentRootFile);
        JScrollPane scrollPane = new JScrollPane(tree);
        // 重新创建顶部面板并添加按钮
        JPanel topPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
        topPanel.add(backOrSelectButton);
        this.add(topPanel, BorderLayout.NORTH);
        this.add(scrollPane, BorderLayout.CENTER);
        revalidate();
        repaint();
    }

    // 判断当前根目录是否为磁盘根目录（兼容Windows和Linux）
    private boolean isRootDirectory(File file) {
        if (file == null) return true;
        File parent = file.getParentFile();
        // Windows下C:\、D:\等磁盘根目录parent为null，Linux下/的parent也为null
        if (parent == null) return true;
        // 兼容Windows下磁盘根目录格式
        String abs = file.getAbsolutePath();
        if (abs.matches("[A-Za-z]:\\\\?")) return true;
        return false;
    }

    public JTree getTree() {
        return tree;
    }

    // 获取当前根目录
    public File getCurrentRootFile() {
        return currentRootFile;
    }
} 